//
//  bofangViewController.m
//  LastTemp
//
//  Created by apple on 15/4/23.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "bofangViewController.h"
#import "AVSingleton.h"
#import "BassModel.h"
#import "drawerView.h"
#import "UIImageView+WebCache.h"
#import "SearchCell.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface bofangViewController ()
@property(nonatomic,retain)UISlider *MySlider;
@property(nonatomic,retain)UIProgressView *progressView;
@property(nonatomic,retain)NSTimer *timer;
@property(nonatomic,retain)UIImageView *cpImageView;
@property(nonatomic,retain)UIButton *playButton;
@property(nonatomic,retain)CABasicAnimation *rotation;
@property(nonatomic,retain)UIButton *sysButton;
@property(nonatomic,retain)UIButton *xysButton;
@property(nonatomic,retain)UIButton *songListButton;

@property(nonatomic,retain)NSMutableArray *songArr;
@property(nonatomic,assign)NSInteger temp;

@property(nonatomic,retain)drawerView *drawer;

@property(nonatomic,retain)UIButton *backLongButton;
@property(nonatomic,retain)UIView *useView;
@property(nonatomic,retain)UIButton *longButton;

@property(nonatomic,retain)UIButton *backButton;


@property(nonatomic,assign)NSInteger theTime;// 1 播放  2暂停  3停止


@property(nonatomic,retain)UILabel *label;
@property(nonatomic,retain)UILabel *labelSinger;

@end

@implementation bofangViewController

- (void)dealloc
{
    [_label release];
    [_labelSinger release];
    [_allArr release];
    [_cpImageView release];
    [_playButton release];
    [_rotation release];
    [_sysButton release];
    [_xysButton release];
    [_songListButton release];
    [_songArr release];
    [_drawer release];
    [_backLongButton release];
    [_useView release];
    [_longButton release];
    [_MySlider release];
    [_progressView release];
    [_listTableView release];
    [super dealloc];
}
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor yellowColor];
   

    
    [self addsongTableView];
    
    self.drawer = [[drawerView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [self.view addSubview:self.drawer];
    [_drawer release];
    self.drawer.backgroundColor = [UIColor cyanColor];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"playBackImage"]];
    imageView.frame = CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height);
    [self.drawer addSubview:imageView];
    [imageView release];

    
    // 上面的横条View
    UIView *longView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 64)];
    [self.drawer addSubview:longView];
    longView.backgroundColor = [UIColor whiteColor];
    [longView release];
    longView.alpha = 0.8;
    longView.tag = 6000;
    
    UIView *playView = [[UIView alloc] initWithFrame:CGRectMake(0, HEIGHT-100, WIDTH, 100)];
    [self.drawer addSubview:playView];
    [playView release];
    playView.backgroundColor = [UIColor grayColor];
    playView.alpha = 0.8;
    
    
    

    self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 64, WIDTH, 20)];
    [self.drawer addSubview:self.label];
    self.label.textAlignment = NSTextAlignmentCenter;
    self.label.font = [UIFont systemFontOfSize:18];
    self.label.textColor = [UIColor whiteColor];
    [_label release];
    
    self.labelSinger = [[UILabel alloc] initWithFrame:CGRectMake(0, 84, WIDTH, 20)];
    [self.drawer addSubview:self.labelSinger];
    self.labelSinger.textAlignment = NSTextAlignmentCenter;
    self.labelSinger.font = [UIFont systemFontOfSize:15];
    self.labelSinger.textColor = [UIColor whiteColor];
    [_labelSinger release];
   
    
    
    

    // 手势
//    UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeActionRight:)];
//    [self.view addGestureRecognizer:swipeRight];
//    swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
    
    UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeActionLeft:)];
    [self.view addGestureRecognizer:swipeLeft];
    swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
    
    // 用户交互
    self.view.userInteractionEnabled = YES;
    
    
    
    
    [AVSingleton shareSingleton].player.delegate = self;
    
    
    self.progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(0, HEIGHT-70-20, self.view.frame.size.width, 10)];
    [self.drawer addSubview:self.progressView];
    self.MySlider = [[UISlider alloc] initWithFrame:CGRectMake(0, 0, self.progressView.frame.size.width, self.progressView.frame.size.height)];
    [self.progressView addSubview:self.MySlider];
    UIImage *sliderImage = [UIImage imageNamed:@"huakuai (1)"];
    [self.MySlider setThumbImage:sliderImage forState:(UIControlStateNormal)];
    self.MySlider.minimumValue = 0;
    self.MySlider.maximumValue = 1;
    self.MySlider.value = [AVSingleton shareSingleton].percentage;
    [self.MySlider addTarget:self action:@selector(myslider:) forControlEvents:(UIControlEventValueChanged)];
    [_MySlider release];
    [_progressView release];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(playProgress:) userInfo:nil repeats:YES];
    
    self.playButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
    self.playButton.frame = CGRectMake(self.view.frame.size.width / 2 - 30, HEIGHT-70, 60, 60);


#pragma mark 唱片旋转
        [self.cpImageView.layer removeAllAnimations];
        CABasicAnimation *rotation1;
        CGFloat duration = 15.0;
        rotation1 = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
        rotation1.duration = duration;
        rotation1.toValue = [NSNumber numberWithFloat:M_PI * 2.0];
        rotation1.cumulative = YES;
        rotation1.repeatCount = HUGE_VALF;
        [self.cpImageView.layer addAnimation:rotation1 forKey:@"rotation"];

    [self.playButton addTarget:self action:@selector(gequClick:) forControlEvents:(UIControlEventTouchUpInside)];
    
   



    
    self.cpImageView = [[UIImageView alloc] init];
    self.cpImageView.image = [UIImage imageNamed:@"cp"];
    self.cpImageView.frame = CGRectMake(self.view.frame.size.width/2 - WIDTH/3, HEIGHT/2-WIDTH/2.5, WIDTH/1.5, WIDTH/1.5);
    [self.drawer addSubview:self.cpImageView];
    [_cpImageView release];


    [self.drawer addSubview:self.playButton];
    
//上一首歌曲button
    self.sysButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
    self.sysButton.frame = CGRectMake(self.view.frame.size.width / 2 - 30 - 80, HEIGHT-70, 60, 60);
    [self.sysButton setBackgroundImage:[UIImage imageNamed:@"shangyishou"] forState:(UIControlStateNormal)];
    [self.sysButton addTarget:self action:@selector(sysClick:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.drawer addSubview:self.sysButton];
    
//下一首button
    self.xysButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
    self.xysButton.frame = CGRectMake(self.view.frame.size.width / 2 - 30 + 80, HEIGHT-70, 60, 60);
    [self.xysButton setBackgroundImage:[UIImage imageNamed:@"xiayishou"] forState:(UIControlStateNormal)];
    [self.xysButton addTarget:self action:@selector(xysClick:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.drawer addSubview:self.xysButton];
    
//歌曲列表button
    self.songListButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
    self.songListButton.frame = CGRectMake(self.view.frame.size.width - 50, HEIGHT-70, 35, 35);
    [self.songListButton setBackgroundImage:[UIImage imageNamed:@"liebiao"] forState:(UIControlStateNormal)];
    [self.songListButton addTarget:self action:@selector(songList:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.drawer addSubview:self.songListButton];

//返回上一页button
    self.backButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
    self.backButton.frame = CGRectMake(20, 20, 40, 40);
    [longView addSubview:self.backButton];
    [self.backButton setBackgroundImage:[UIImage imageNamed:@"fanhui"] forState:UIControlStateNormal];

    [self.backButton addTarget:self action:@selector(backClick) forControlEvents:(UIControlEventTouchUpInside)];
    self.backButton.tag = 3000;
    

    
    
    // longView 上下拉按钮
    self.longButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [longView addSubview:self.longButton];
    self.longButton.frame = CGRectMake(WIDTH-55, 20, 40, 40);
    [self.longButton setBackgroundImage:[UIImage imageNamed:@"xia"] forState:UIControlStateNormal];
    [self.longButton addTarget:self action:@selector(longClick:) forControlEvents:UIControlEventTouchUpInside];
    
    
    self.backLongButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [longView addSubview:self.backLongButton];
    self.backLongButton.frame = CGRectMake(WIDTH-55, 20, 40, 40);
    [self.backLongButton setBackgroundImage:[UIImage imageNamed:@"shang"] forState:UIControlStateNormal];
    [self.backLongButton addTarget:self action:@selector(backLongClick:) forControlEvents:UIControlEventTouchUpInside];
    self.backLongButton.hidden = YES;

}

- (void)longClick:(UIButton *)button
{
    self.useView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 64)];
    [self.drawer addSubview:self.useView];
    [_useView release];
    self.useView.backgroundColor = [UIColor grayColor];
   
    
        [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.1 initialSpringVelocity:1 options:UIViewAnimationOptionCurveEaseInOut animations:^{
             self.useView.frame = CGRectMake(0, 64, WIDTH, 64);
        } completion:^(BOOL finished) {
            UIButton *shareButton = [UIButton buttonWithType:UIButtonTypeSystem];
            shareButton.frame = CGRectMake(WIDTH/2+100-40, 12, 40, 40);
            [shareButton setBackgroundImage:[UIImage imageNamed:@"fengxianglast"] forState:UIControlStateNormal];
            [self.useView addSubview:shareButton];
            [shareButton addTarget:self action:@selector(clickShare:) forControlEvents:UIControlEventTouchUpInside];
            
            
            //收藏button
            UIButton *collectButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
            collectButton.frame = CGRectMake(WIDTH/2-100, 12, 40, 40);
            [collectButton addTarget:self action:@selector(collectClick:) forControlEvents:(UIControlEventTouchUpInside)];
            [collectButton setBackgroundImage:[UIImage imageNamed:@"shoucanglast"] forState:(UIControlStateNormal)];
            [self.useView addSubview:collectButton];

    
        }];
    
    
    button.hidden = YES;
    self.backLongButton.hidden = NO;
    
    
}

- (void)backLongClick:(UIButton *)button
{
    [self.useView removeFromSuperview];
    self.longButton.hidden = NO;
    button.hidden = YES;
    
}



//收藏点击方法
-(void)collectClick:(UIButton *)collect
{
    
    if ([AVSingleton shareSingleton].fromArr.count > 0) {
   
//开启数据库
    NSArray *sandArr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *sandPath = [sandArr lastObject];
    NSString *dbPath = [sandPath stringByAppendingPathComponent:@"music.sqlite"];
    NSLog(@"%@",dbPath);
    int result = sqlite3_open([dbPath UTF8String], &dbPoint);
    if (result == SQLITE_OK) {
        NSLog(@"数据库开启成功");
    }else{
        NSLog(@"数据库开启失败");
    }
  
//创建表
    NSString *sqlStr = @"create table if not exists music(number integer primary key autoincrement,song_name text,singer_name text,url text)";
    int result2 = sqlite3_exec(dbPoint, [sqlStr UTF8String], nil, nil, nil);
    if (result2 == SQLITE_OK) {
        NSLog(@"创建表成功");
    }else{
        NSLog(@"创建表失败");
    }
    
   

    
   
    self.songArr = [NSMutableArray array];
    
    
    NSString *selectAll = @"select *from music";
    sqlite3_stmt *stmt= nil;
    int result3 = sqlite3_prepare_v2(dbPoint, [selectAll UTF8String], -1, &stmt, nil);
    if (result3 == SQLITE_OK) {
        NSLog(@"查询成功");
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            const unsigned char *song_name = sqlite3_column_text(stmt, 1);
            const unsigned char *singer_name = sqlite3_column_text(stmt, 2);
            const unsigned char *url = sqlite3_column_text(stmt, 3);
            NSString *song = [NSString stringWithUTF8String:(const char *)song_name];
            NSString *singername = [NSString stringWithUTF8String:(const char *)singer_name];
            NSString *songUrl = [NSString stringWithUTF8String:(const char *)url];
             BassModel *base = [[BassModel alloc] init];
            base.name1 = song;
            base.singerName1 = singername;
            base.url1 = songUrl;
            [self.songArr addObject:base];
        }
    }

    // 判断要收藏的歌曲是否已经收藏
    BassModel *base = [AVSingleton shareSingleton].fromArr[[AVSingleton shareSingleton].fromNum];
    if (self.songArr.count > 0) {
        for (NSInteger i = 0; i < self.songArr.count; i++) {
            BassModel *bas = self.songArr[i];
            
            if ([bas.name1 isEqualToString:base.name1]) {
                [self showMessage:@"已收藏"];
                NSLog(@"已经收藏");
                self.temp = 2;
                break;
            } else {
                self.temp = 1;
            }
        }

    } else {
        self.temp = 1;
}
    
    
    

    if (self.temp == 1) {
        NSString *insertStr = [NSString stringWithFormat:@"insert into music(song_name,singer_name,url) values('%@','%@','%@')",base.name1,base.singerName1,base.url1];
        int result4 = sqlite3_exec(dbPoint, [insertStr UTF8String], nil, nil, nil);
        if (result4 == SQLITE_OK) {
            NSLog(@"插入成功");
            [collect setBackgroundImage:[UIImage imageNamed:@"shoucanglast"] forState:(UIControlStateNormal)];
            [self showMessage:@"收藏成功"];
            self.temp = 0;
        }else{
            NSLog(@"插入失败");
       }
    }
    } else {
        [self showMessage:@"没有歌曲不能收藏"];
    }

}

#pragma mark 分享点击方法
- (void)clickShare:(UIButton *)button
{
    
    BassModel *base = [AVSingleton shareSingleton].fromArr[[AVSingleton shareSingleton].fromNum];
    NSString *shareStr = [NSString stringWithFormat:@"我在用草莓音乐听<<%@>>,你也一起来吧!",base.name1];
    [UMSocialSnsService presentSnsIconSheetView:self appKey:@"553afad967e58e7cba0031e4" shareText:shareStr shareImage:[UIImage imageNamed:@"UMS_account_normal_white"] shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,UMShareToRenren,UMShareToQQ, nil] delegate:self];
}


-(void)backClick
{
    
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
    [AVSingleton shareSingleton].playNum = 1;
    [AVSingleton shareSingleton].playNum2 = 1;
    
    
   

}

//播放暂停
-(void)gequClick:(UIButton *)bu
{
    if ([AVSingleton shareSingleton].myBool == NO) {
        [[AFSoundManager sharedManager].player play];
        [self.playButton setBackgroundImage:[UIImage imageNamed:@"bofang"] forState:(UIControlStateNormal)];
        
#pragma mark 唱片旋转
        [self.cpImageView.layer removeAllAnimations];
        CGFloat duration = 15.0;
        self.rotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
        self.rotation.duration = duration;
        self.rotation.toValue = [NSNumber numberWithFloat:M_PI * 2.0];
        self.rotation.cumulative = YES;
        self.rotation.repeatCount = HUGE_VALF;
        [self.cpImageView.layer addAnimation:self.rotation forKey:@"rotation"];
        [AVSingleton shareSingleton].myBool = YES;

    }else{
        [[AFSoundManager sharedManager].player pause];
        CGFloat duration = 100000000000.0;
        
        self.rotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
        self.rotation.duration = duration;
        self.rotation.toValue = [NSNumber numberWithFloat:M_PI * 2.0];
        self.rotation.cumulative = YES;
        self.rotation.repeatCount = HUGE_VALF;
        [self.cpImageView.layer addAnimation:self.rotation forKey:@"rotation"];

        [self.playButton setBackgroundImage:[UIImage imageNamed:@"zanting"] forState:(UIControlStateNormal)];
        [AVSingleton shareSingleton].myBool = NO;

    }
}

//控制歌曲播放进度
-(void)myslider:(UISlider *)slider
{

    [[AFSoundManager sharedManager] moveToSection:slider.value];

}

-(void)playProgress:(id)sender
{
    if ([[AVSingleton shareSingleton].bigButton isEqual:[AVSingleton shareSingleton].time] && [AVSingleton shareSingleton].myBool == YES) {
        [self cpPlay];

    }

    if ([[AVSingleton shareSingleton].beginTime isEqualToString:@"00:01"]) {
        [self cpPlay];
        [AVSingleton shareSingleton].beginTime = @"haha";
        
    }

    
    self.progressView.progress = [AVSingleton shareSingleton].percentage*0.01;
    self.MySlider.value = [AVSingleton shareSingleton].percentage*0.01;
    
    if ([[AVSingleton shareSingleton].time isEqualToString:@"00:01"]) {
        
        if ([AVSingleton shareSingleton].fromNum < [AVSingleton shareSingleton].fromArr.count-1) {
            [AVSingleton shareSingleton].fromNum++;
            [[AFSoundManager sharedManager] stop];
            
            BassModel *base = [AVSingleton shareSingleton].fromArr[[AVSingleton shareSingleton].fromNum];
            [[AFSoundManager sharedManager] startStreamingRemoteAudioFromURL:base.url1 andBlock:^(int percentage, CGFloat elapsedTime, CGFloat timeRemaining, NSError *error, BOOL finished) {
                
                [AVSingleton shareSingleton].percentage = percentage;
                NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
                [formatter setDateFormat:@"mm:ss"];
                
                NSDate *timeRemainingDate = [NSDate dateWithTimeIntervalSince1970:timeRemaining];
                [AVSingleton shareSingleton].time = [formatter stringFromDate:timeRemainingDate];
            }];
            
            [self.cpImageView.layer removeAllAnimations];
            CGFloat duration = 15.0;
            self.rotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
            self.rotation.duration = duration;
            self.rotation.toValue = [NSNumber numberWithFloat:M_PI * 2.0];
            self.rotation.cumulative = YES;
            self.rotation.repeatCount = HUGE_VALF;
            [self.cpImageView.layer addAnimation:self.rotation forKey:@"rotation"];
            [AVSingleton shareSingleton].myBool = YES;
            
            [AVSingleton shareSingleton].time = @"hahah";
        } else {
            [AVSingleton shareSingleton].fromNum = 0;
            [[AFSoundManager sharedManager] pause];
        }
     
        
        
    }
   

    
    if ([AVSingleton shareSingleton].myBool == YES) {
                [self.playButton setBackgroundImage:[UIImage imageNamed:@"bofang"] forState:(UIControlStateNormal)];
    } else {
                [self.playButton setBackgroundImage:[UIImage imageNamed:@"zanting"] forState:(UIControlStateNormal)];
    }
    
    BassModel *base = [AVSingleton shareSingleton].fromArr[[AVSingleton shareSingleton].fromNum];
    self.label.text = base.name1;
    self.labelSinger.text = base.singerName1;
    
}


#pragma mark 上一首点击事件
-(void)sysClick:(UIButton *)sys
{
    [[AFSoundManager sharedManager] stop];
    if ([AVSingleton shareSingleton].fromNum >= 1) {
        [AVSingleton shareSingleton].fromNum--;
    }

    BassModel *base = [AVSingleton shareSingleton].fromArr[[AVSingleton shareSingleton].fromNum];
    [[AFSoundManager sharedManager] startStreamingRemoteAudioFromURL:base.url1 andBlock:^(int percentage, CGFloat elapsedTime, CGFloat timeRemaining, NSError *error, BOOL finished) {
        
        [AVSingleton shareSingleton].percentage = percentage;
        NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
        [formatter setDateFormat:@"mm:ss"];
        
        NSDate *timeRemainingDate = [NSDate dateWithTimeIntervalSince1970:timeRemaining];
        [AVSingleton shareSingleton].time = [formatter stringFromDate:timeRemainingDate];

    }];
    [self.cpImageView.layer removeAllAnimations];
    CGFloat duration = 15.0;
    self.rotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    self.rotation.duration = duration;
    self.rotation.toValue = [NSNumber numberWithFloat:M_PI * 2.0];
    self.rotation.cumulative = YES;
    self.rotation.repeatCount = HUGE_VALF;
    [self.cpImageView.layer addAnimation:self.rotation forKey:@"rotation"];
    [AVSingleton shareSingleton].myBool = YES;
 
}

#pragma mark 下一首
-(void)xysClick:(UIButton *)xys
{
    
    
    if ([AVSingleton shareSingleton].fromNum < [AVSingleton shareSingleton].fromArr.count-1) {
        [AVSingleton shareSingleton].fromNum++;
[[AFSoundManager sharedManager] stop];
    BassModel *base = [AVSingleton shareSingleton].fromArr[[AVSingleton shareSingleton].fromNum];
    [[AFSoundManager sharedManager] startStreamingRemoteAudioFromURL:base.url1 andBlock:^(int percentage, CGFloat elapsedTime, CGFloat timeRemaining, NSError *error, BOOL finished) {
        [AVSingleton shareSingleton].percentage = percentage;
        NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
        [formatter setDateFormat:@"mm:ss"];
        
        NSDate *timeRemainingDate = [NSDate dateWithTimeIntervalSince1970:timeRemaining];
        [AVSingleton shareSingleton].time = [formatter stringFromDate:timeRemainingDate];
    }];
    
    [self.cpImageView.layer removeAllAnimations];
    CGFloat duration = 15.0;
    self.rotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    self.rotation.duration = duration;
    self.rotation.toValue = [NSNumber numberWithFloat:M_PI * 2.0];
    self.rotation.cumulative = YES;
    self.rotation.repeatCount = HUGE_VALF;
    [self.cpImageView.layer addAnimation:self.rotation forKey:@"rotation"];
    [AVSingleton shareSingleton].myBool = YES;
    } else {
        [self showMessage:@"最后一曲"];
    }

  

}
#pragma mark songListButton方法
-(void)songList:(UIButton *)songButton
{

    [UIView animateWithDuration:0.5 animations:^{
        self.drawer.frame = CGRectMake(250, 100, WIDTH, HEIGHT-200);
    } completion:^(BOOL finished) {
        self.drawer.frame = CGRectMake(250, 100, WIDTH, HEIGHT-200);
        
        self.backButton.hidden = YES;
    }];
    
    
}

-(void)creatTableView
{
    self.listTableView = [[UITableView alloc] initWithFrame: CGRectMake(0, 20, 200, self.view.frame.size.height-130)];
    [self.drawer addSubview:self.listTableView];
    self.listTableView.dataSource = self;
    self.listTableView.delegate = self;
    [_listTableView release];
    
  
}



-(void)cpPlay
{
#pragma mark 唱片旋转
    [self.cpImageView.layer removeAllAnimations];
    CGFloat duration = 15.0;
    self.rotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    self.rotation.duration = duration;
    self.rotation.toValue = [NSNumber numberWithFloat:M_PI * 2.0];
    self.rotation.cumulative = YES;
    self.rotation.repeatCount = HUGE_VALF;
    [self.cpImageView.layer addAnimation:self.rotation forKey:@"rotation"];
    
}


-(void)showMessage:(NSString *)message
{
    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2-75, self.view.frame.size.height/2-32.5, 150, 65)];
    view2.backgroundColor = [UIColor whiteColor];
    view2.alpha = 0.8;
    UILabel *label = [[UILabel alloc] init];
    label.text = message;
    label.textAlignment = NSTextAlignmentCenter;
    label.frame = CGRectMake(view2.frame.size.width/2-50, view2.frame.size.height/2-20, 100, 40);
    [view2 addSubview:label];
    
    [self.drawer addSubview:view2];
    [UIView animateWithDuration:2 animations:^{
    view2.alpha = 0;
    } completion:^(BOOL finished) {
    [view2 removeFromSuperview];
    }];
    
    [label release];
    [view2 release];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



//- (void)swipeActionRight:(UISwipeGestureRecognizer *)swipe{
//    
//    [UIView animateWithDuration:0.25 animations:^{
//        self.drawer.frame = CGRectMake(250, 100, WIDTH, HEIGHT-200);
//                NSLog(@"/////");
//    } completion:^(BOOL finished) {
//    }];
//}
- (void)swipeActionLeft:(UISwipeGestureRecognizer *)swipe{
    [UIView animateWithDuration:0.25 animations:^{
        self.drawer.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
        
        NSLog(@"+++++");
    } completion:^(BOOL finished) {
        self.backButton.hidden = NO;
    }];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/



- (void)addsongTableView
{
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    [self.view addSubview:imageView];
    [imageView release];
    imageView.image = [UIImage imageNamed:@"9"];
    
    
    
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 30, WIDTH, HEIGHT-30) style:UITableViewStylePlain];
    [self.view addSubview:tableView];
    [tableView release];
    tableView.backgroundColor = [UIColor clearColor];
    
    tableView.rowHeight = 70;
    tableView.dataSource = self;
    tableView.delegate = self;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [AVSingleton shareSingleton].fromArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse = @"reuse";
    SearchCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (cell == nil) {
        cell = [[[SearchCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];
    }
    BassModel *base = [AVSingleton shareSingleton].fromArr[indexPath.row];
    cell.nameLabel.text = base.name1;
    cell.singerLabel.text = base.singerName1;
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    BassModel *base = [AVSingleton shareSingleton].fromArr[indexPath.row];
    
    
    if ([AVSingleton shareSingleton].fromNum != indexPath.row || [AVSingleton shareSingleton].fromNum != 0) {
        [[AFSoundManager sharedManager] stop];
    }
    [[AFSoundManager sharedManager] startStreamingRemoteAudioFromURL:base.url1 andBlock:^(int percentage, CGFloat elapsedTime, CGFloat timeRemaining, NSError *error, BOOL finished) {
        [AVSingleton shareSingleton].percentage = percentage;
        NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
        [formatter setDateFormat:@"mm:ss"];
        NSDate *elapsedTimeDate = [NSDate dateWithTimeIntervalSince1970:elapsedTime];
        [AVSingleton shareSingleton].beginTime = [formatter stringFromDate:elapsedTimeDate];
        
        NSDate *timeRemainingDate = [NSDate dateWithTimeIntervalSince1970:timeRemaining];
        [AVSingleton shareSingleton].time = [formatter stringFromDate:timeRemainingDate];

    }];
    
    [AVSingleton shareSingleton].fromNum = indexPath.row;
    [AVSingleton shareSingleton].myBool = YES;
    [UIView animateWithDuration:0.25 animations:^{
        self.drawer.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
        
        NSLog(@"+++++");
    } completion:^(BOOL finished) {
        self.backButton.hidden = NO;
    }];

    
}


@end
